import { Color, Sprite, Vec3 } from 'cc';
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

export default class Design {
    // 最多显示对数
    public static maxShowPairCount: number = 25
    // 卡牌的基础缩放
    public static baseScale: Vec3 = new Vec3(1, 1, 1)
    public static slotScale: Vec3 = new Vec3(0.7, 0.7, 0.7)

    // 颜色
    static whiteColor = new Color(255, 255, 255, 255)
    static grayColor = new Color(175, 175, 175, 255)

    // 游戏其他配置
    static gameConfig = {
        tipShowDuration: 2
    }

    // 相关动画配置
    static cardAnimConfig = {
        // 从世界飞到选择槽的时间
        worldToSlotDuration: 0.15,
        // 发牌间隔时间
        sendCardSpaceDuration: 0.04
    }
    
    // 移动到其他的父节点，同时保持在屏幕上的位置不变
    public static changeParenWithoutMove(node: Node, newParent: Node) {
        // 获取目标节点的世界坐标
        const worldPosition = new Vec3();
        node.getWorldPosition(worldPosition);

        // 将目标节点设置为新的父节点的子节点
        node.setParent(newParent);

        // 将世界坐标转换为新的父节点的局部坐标
        const localPosition = new Vec3();
        if (newParent != null)
            newParent.inverseTransformPoint(localPosition, worldPosition);

        // 设置目标节点的局部坐标
        node.setPosition(localPosition);
    }

    // 获取一个节点相对于另一个节点的位置
    public static getRelativePosition(source: Node, target: Node): Vec3 {
        // 获取 source 节点的世界坐标
        const worldPosition = new Vec3();
        source.getWorldPosition(worldPosition);

        // 将世界坐标转换为 target 节点的局部坐标
        const localPosition = new Vec3();
        target.inverseTransformPoint(localPosition, worldPosition);

        return localPosition;
    }

    // 置灰
    public static grayOutNode(node: Node) {
        // 遍历所有子节点并置灰
        node.getComponent(Sprite).color = Design.grayColor;
        for (var i = 0; i < node.children.length; i++) {
            const childNode = node.children[i]
            if (childNode.getComponent(Sprite) ) {
                childNode.getComponent(Sprite).color = Design.grayColor;
            }
        }
    }

    // 置白
    public static whiteOutNode(node: Node) {
        node.getComponent(Sprite).color = Design.whiteColor;
        for (var i = 0; i < node.children.length; i++) {
            const childNode = node.children[i]
            if (childNode.getComponent(Sprite) ) {
                childNode.getComponent(Sprite).color = Design.whiteColor;
            }
        }
    }
}
